#include "texture.h"

#include "Interval.h"

SolidColor::SolidColor(double red, double green, double blue)
	: SolidColor(Color(red, green, blue)) {}

ImageTexture::ImageTexture(const char* filename) : image(filename) {}

Color ImageTexture::value(double u, double v, const Point3& p) const
{
    // If we have no texture data, then return solid cyan as a debugging aid.
    if (image.height() <= 0) return Color(0, 1, 1);

    // Clamp input texture coordinates to [0,1] x [1,0]
    u = Interval(0, 1).clamp(u);
    v = 1.0 - Interval(0, 1).clamp(v);  // Flip V to image coordinates

    auto i = int(u * image.width());
    auto j = int(v * image.height());
    auto pixel = image.pixel_data(i, j);

    auto color_scale = 1.0 / 255.0;
    return Color(color_scale * pixel[0], color_scale * pixel[1], color_scale * pixel[2]);
}

NoiseTexture::NoiseTexture(double scale):scale(scale)
{
}

Color NoiseTexture::value(double u, double v, const Point3& p) const
{
    return Color(1, 1, 1) * noise.noise(scale * p);
}
